package com.egoo.ticket.server.controller;

import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.egoo.ticket.server.pojo.dto.ReportDataDto;
import com.egoo.ticket.server.pojo.vo.ReportDataExcelForCustomVo;
import com.egoo.ticket.server.pojo.vo.ReportDataExcelVo;
import com.egoo.ticket.server.service.ReportDataService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ExcelUtil;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Donny_dong
 */
@Slf4j
@RestController
@RequestMapping(value = "/v1/reportdata", produces = "application/json")
@Api(value = "报表数据管理",tags = "报表数据管理",description = "报表数据管理")
public class ReportDataController {

	@Autowired
	private ReportDataService reportDataService;

	@ApiOperation(value = "总数统计", notes = "总数统计")
	@RequestMapping(method = RequestMethod.POST, value="/getTotal")
	public DbResponse getTotal(@ApiParam(value="getTotal", required=true)
											  @RequestBody ReportDataDto reportDataDto,
							   HttpServletRequest request){
//		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
//		tenantId = "boc";
//		if (StringUtils.isEmpty(userId)) {
//			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"用户id不能为空！");
//		}
		if (StringUtils.isEmpty(tenantId)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"租户id不能为空！");
		}
		reportDataDto.setUserId(reportDataDto.getUserId());
		reportDataDto.setTenantId(tenantId);
		try {
			return reportDataService.getTotalByCondition(reportDataDto);
		}catch (Exception e){
			log.info("获取报表总数数据异常："+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
					ResponseConstant.SELECT_FAIL_MSG,"获取报表总数数据异常:"+e);
		}
	}
	@ApiOperation(value = "详情列表统计", notes = "详情列表统计")
	@RequestMapping(method = RequestMethod.POST, value=
			"/getDetailList")
	public DbResponse getDetailList(@ApiParam(value="getDetailList", required=true)
										  @RequestBody ReportDataDto reportDataDto,
										  HttpServletRequest request){
//		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
//		tenantId = "boc";
//		if (StringUtils.isEmpty(userId)) {
//			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"用户id不能为空！");
//		}
		if (StringUtils.isEmpty(tenantId)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"租户id不能为空！");
		}
		reportDataDto.setUserId(reportDataDto.getUserId());
		reportDataDto.setTenantId(tenantId);
		try {
			return reportDataService.getDetailListByCondition(reportDataDto);
		}catch (Exception e){
			log.info("获取报表详情列表数据异常："+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
					ResponseConstant.SELECT_FAIL_MSG,"获取报表详情列表数据异常:"+e);
		}
	}

	@ApiOperation(value = "客服工单列表统计", notes = "客服工单列表统计")
	@RequestMapping(method = RequestMethod.POST, value=
			"/getCreaterList")
	public DbResponse getCreaterList(@RequestBody ReportDataDto reportDataDto, HttpServletRequest request){

		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
//		tenantId = "boc";

		if (StringUtils.isEmpty(tenantId)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"租户id不能为空!");
		}
		reportDataDto.setUserId(userId);
		reportDataDto.setTenantId(tenantId);
		return reportDataService.getCreaterList(reportDataDto);

	}

	@ApiOperation(value = "综合图表数据", notes = "综合图表数据")
	@RequestMapping(method = RequestMethod.POST, value=
			"/getGraphicsDate")
	public DbResponse getGraphicsDate(@RequestBody ReportDataDto reportDataDto, HttpServletRequest request){

		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
//		tenantId = "boc";

		if (StringUtils.isEmpty(tenantId)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"租户id不能为空!");
		}
		reportDataDto.setUserId(reportDataDto.getUserId());
		reportDataDto.setTenantId(tenantId);
		return reportDataService.getGraphicsListByCondition(reportDataDto);

	}

	@ApiOperation(value = "客户列表统计导出", notes = "客户列表统计导出")
	@RequestMapping(method = RequestMethod.POST, value=
			"/createrListForExcel")
	public void createrListForExcel(@ApiParam(value="createrListForExcel", required=true)
									@RequestBody ReportDataDto reportDataDto,
                                    HttpServletRequest request, HttpServletResponse response){
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
//		tenantId = "boc";

		reportDataDto.setUserId(reportDataDto.getUserId());
		reportDataDto.setTenantId(tenantId);
		try {
			ArrayList<ReportDataExcelForCustomVo> reportDataExcelForCustomVos = reportDataService.getcreaterListForExcel(reportDataDto);
			Map<String, List<? extends BaseRowModel>> map = new HashMap<>(2);
			// 支持多sheet导出，一个map元素代表一个sheet
			map.put("导出数据信息", reportDataExcelForCustomVos);
			ExcelUtil.createExcelStreamMutilByEaysExcel(response, map,
					ExcelTypeEnum.XLSX, System.currentTimeMillis()+".xlsx",
					ReportDataExcelForCustomVo.class);
		}catch (Exception e){
			log.info("导出数据异常："+e);

		}

	}

	@ApiOperation(value = "综合列表统计导出", notes = "综合列表统计导出")
	@RequestMapping(method = RequestMethod.POST, value=
			"/detailListForExcel")
	public void detailListForExcel(@ApiParam(value="detailListForExcel", required=true)
								   @RequestBody ReportDataDto reportDataDto,
                                   HttpServletRequest request, HttpServletResponse response){
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
//		tenantId = "boc";

		reportDataDto.setUserId(reportDataDto.getUserId());
		reportDataDto.setTenantId(tenantId);
		try {
			ArrayList<ReportDataExcelVo> reportDataExcelVos = reportDataService.getDetailListForExcel(reportDataDto);
			Map<String, List<? extends BaseRowModel>> map = new HashMap<>(2);
			// 支持多sheet导出，一个map元素代表一个sheet
			map.put("导出数据信息", reportDataExcelVos);
			ExcelUtil.createExcelStreamMutilByEaysExcel(response, map,
					ExcelTypeEnum.XLSX, System.currentTimeMillis()+".xlsx",
					ReportDataExcelVo.class);
		}catch (Exception e){
			log.info("导出数据异常："+e);

		}

	}

}
